<?php
namespace api\modules\mp\models;

use yii\base\Model;
use common\tool\Curd;
use yii\web\NotFoundHttpException;

class Boat extends Model {
    use Curd;
    const CLIENT_FIELDS = [
        'chinese_name',
        'english_name',
        'birthday',
        'gender',
        'phone',
        'email',
        'wechat',
        'passport',
        'id_number'
    ];

    public static function tableName() {
        return 'mp_boat';
    }

    public function rules() {
        return [
            [
                [
                    'title',
                    'stime',
                    'etime',
                    'days',
                    'nights',
                    'dives',
                    'pics',
                    'place',
                ],
                'required'
            ],
            [
                [
                    'title',
                    'openid',
                    'requireLevel',
                    'requireBottle',
                    'place',
                    'content',
                ],
                'string'
            ],
            [
                [
                    'stime',
                    'etime'
                ],
                'datetime',
                'format' => 'yyyy-MM-dd HH:mm:ss'
            ],
            [
                [
                    'days',
                    'nights',
                    'dives',
                ],
                'number',
                'min' => 0
            ],
            [
                'personNum',
                'number',
                'min' => 1
            ],
            [
                ['hasNight'],
                'boolean'
            ],
            [
                'clientFields',
                'each',
                'rule' => [
                    'in',
                    'range' => self::CLIENT_FIELDS
                ]
            ]
        ];
    }
    public function getAttributeLabel($attribute) {
        return [

        ];
    }

    public static function build($data = []): ?self {
        if (!empty($data['id'])) {
            $one = self::getOne("id={$data['id']}");
            if (!$one) throw new NotFoundHttpException();
            $data = array_merge($one, $data);
        }
        return new self($data);
    }

    public $id;
    public $title;
    public $openid;
    public $stime;
    public $etime;
    public $days;
    public $nights = 0;
    public $dives;
    public $hasNight=0;
    public $requireLevel='';
    public $requireBottle='';
    public $personNum;
    public $place;
    public $pics;
    public $descriptions = '';
    public $content;
    public $clientFields = [];
    public $ctime;
    public $utime;
    public $status = self::STATUS_PUBLISH;
    const STATUS_DRAFT = 1;
    const STATUS_PUBLISH = 2;
    const STATUS_CANCEL = 3;

    public function attributeLabels() {
        return [
            'title' => '标题',
            'stime' => '出行开始时间',
            'etime' => '出行结束时间',
            'days' => '天数',
            'nights' => '夜数',
            'dives' => '潜次',
            'hasNight' => '包含夜潜',
            'requireLevel' => '潜水要求',
            'requireBottle' => '潜水要求',
            'place' => '地点',
            'content' => '内容',
            'pics' => '图片',
            'personNum' => '报名人数',
        ];
    }
    public function setHas_night($v){$this->hasNight=$v;}
    public function setRequire_level($v){$this->requireLevel=$v;}
    public function setRequire_bottle($v){$this->requireBottle=$v;}
    public function setPerson_num($v){$this->personNum=$v;}
    public function setClient_fields($v) {$this->clientFields = $v; }

    public function save() {
        $this->clientFields = json_encode($this->clientFields);
        $data = [
            'title' => $this->title,
            'stime' => $this->stime,
            'etime' => $this->etime,
            'days'=>$this->days,'nights'=>$this->nights,'dives'=>$this->dives,'has_night'=>$this->hasNight,
            'require_level'=>$this->requireLevel,'require_bottle'=>$this->requireBottle,
            'content'=>$this->content,
            'openid' => $this->openid,
            'descriptions' => $this->descriptions,
            'client_fields'=>$this->clientFields,
            'place' => $this->place,
            'pics' => $this->pics ?: '',
            'status' => $this->status,
        ];
        $now = date('Y-m-d H:i:s');
        if ($this->id) {
            $data['utime'] = $now;
            self::update($data, 'id=:id', [':id' => $this->id]);
        } else {
            $data['ctime'] = $data['utime'] = $now;
            $id = self::insert($data);
            $this->id = $id;
        }
    }

    public function publish() {
        return self::update(['status' => self::STATUS_PUBLISH], 'id=:id', [':id' => $this->id]);
    }

    public function cancel() {
        return self::update(['status' => self::STATUS_CANCEL], 'id=:id', [':id' => $this->id]);
    }

    public function info() {
        $r=$this->getAttributes();
        $r['clientFields']=json_decode($r['clientFields'],true);
        return $r;
    }
}
